package com.chii.image.controller;

import com.chii.image.po.ResponeInfo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@ControllerAdvice
public class GlobalExceptionResolver {
    private Logger logger = LogManager.getLogger(GlobalExceptionResolver.class);

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponeInfo handleException(HttpServletRequest request,
                                       HttpServletResponse response, Object handler, Exception e) {
        String info = "getRequestURL:" + request.getRequestURL() +
                "--getQueryString:" + request.getQueryString() +
                "--getRemoteAddr:" + request.getRemoteAddr() +
                "--getRemoteHost:" + request.getRemoteHost() +
                "--getRemotePort:" + request.getRemotePort() + "\n";
        // 打印异常堆栈信息
        logger.error(info, e);
        ResponeInfo responeInfo = new ResponeInfo();
        responeInfo.failure(e.getMessage());
        return responeInfo;
    }

}
